{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-02-26T12:59:10.230802Z",
     "start_time": "2025-02-26T12:59:09.510992Z"
    }
   },
   "source": "import pandas as pd",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "# Series",
   "id": "127f361c26741270"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T12:59:41.534886Z",
     "start_time": "2025-02-26T12:59:41.525722Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 生成一个Series\n",
    "#是一个一维标记数组，能够保存任何数据类型（整数、字符串、浮点数、Python 对象等）\n",
    "ser_obj = pd.Series(range(10, 20)) #默认索引是0-9,生成10-19\n",
    "print(ser_obj) #打印输出会带有类型\n",
    "print('-'*50)\n"
   ],
   "id": "c5dc9c7e5d981f81",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "3    13\n",
      "4    14\n",
      "5    15\n",
      "6    16\n",
      "7    17\n",
      "8    18\n",
      "9    19\n",
      "dtype: int64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:03:24.356029Z",
     "start_time": "2025-02-26T13:03:24.266280Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取数据\n",
    "print(ser_obj.values)  #values实际是ndarray\n",
    "print(type(ser_obj.values)) #类型是ndarray\n",
    "# 获取索引\n",
    "print(ser_obj.index)  #内部自带的类型--RangeIndex\n",
    "ser_obj.dtype #数据类型"
   ],
   "id": "5b6695462465ce09",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 11 12 13 14 15 16 17 18 19]\n",
      "<class 'numpy.ndarray'>\n",
      "RangeIndex(start=0, stop=10, step=1)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:05:29.253343Z",
     "start_time": "2025-02-26T13:05:29.219268Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 访问不存在的索引下标会报keyerror\n",
    "print(ser_obj[0]) #打印索引为0的元素\n",
    "ser_obj[9]"
   ],
   "id": "96eba2d9df36744f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "np.int64(19)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:06:25.344018Z",
     "start_time": "2025-02-26T13:06:25.263382Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(ser_obj * 2)  #元素级乘法\n",
    "print(ser_obj > 15) #返回一个bool序列,满足条件的为True,否则Flase"
   ],
   "id": "5492203b89e408b3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    20\n",
      "1    22\n",
      "2    24\n",
      "3    26\n",
      "4    28\n",
      "5    30\n",
      "6    32\n",
      "7    34\n",
      "8    36\n",
      "9    38\n",
      "dtype: int64\n",
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5    False\n",
      "6     True\n",
      "7     True\n",
      "8     True\n",
      "9     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:11:49.178339Z",
     "start_time": "2025-02-26T13:11:49.151228Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#字典变为series，索引是字典的key，value是字典的value，感受非默认索引\n",
    "year_data = {2001: 17.8, 2005: 20.1, 2003: 16.5}\n",
    "ser_obj2 = pd.Series(year_data) #字典变为series\n",
    "print(ser_obj2)\n",
    "print('-'*50)\n",
    "print(ser_obj2.index) #打印索引及其dtype\n",
    "print(ser_obj2[2001]) #打印索引为2001的元素\n",
    "ser_obj2.values"
   ],
   "id": "59ed0ab2305ace7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2001    17.8\n",
      "2005    20.1\n",
      "2003    16.5\n",
      "dtype: float64\n",
      "--------------------------------------------------\n",
      "Index([2001, 2005, 2003], dtype='int64')\n",
      "17.8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([17.8, 20.1, 16.5])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 通过ndarray构建DataFrame",
   "id": "33ae06be9955206d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:15:00.321152Z",
     "start_time": "2025-02-26T13:15:00.240075Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "# DataFrame是一个二维标记数组，能够保存任何数据类型（整数、字符串、浮点数、Python 对象等）\n",
    "t = pd.DataFrame(np.arange(12).reshape((3,4))) #默认索引是0-2\n",
    "print(t)\n",
    "print('-'*50)\n",
    "array = np.random.randn(5,4) #生成5行4列的随机数\n",
    "print(array)\n",
    "print('-'*50)\n",
    "df_obj = pd.DataFrame(array) #ndarray变为DataFrame\n",
    "print(df_obj.head())\n",
    "print('-'*50)\n",
    "df_obj"
   ],
   "id": "a0a3df25bb7354f0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0  1   2   3\n",
      "0  0  1   2   3\n",
      "1  4  5   6   7\n",
      "2  8  9  10  11\n",
      "--------------------------------------------------\n",
      "[[-1.89015074  0.57106106  0.55986951  0.03724696]\n",
      " [-0.94831577  0.44835004 -0.17236736 -1.3153982 ]\n",
      " [-0.85170469 -0.40488019 -1.1662173  -1.94825663]\n",
      " [-0.69222181 -0.60597359  0.19426094 -1.23997874]\n",
      " [-0.61770311  0.88626376  0.49581533  1.16221626]]\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0 -1.890151  0.571061  0.559870  0.037247\n",
      "1 -0.948316  0.448350 -0.172367 -1.315398\n",
      "2 -0.851705 -0.404880 -1.166217 -1.948257\n",
      "3 -0.692222 -0.605974  0.194261 -1.239979\n",
      "4 -0.617703  0.886264  0.495815  1.162216\n",
      "--------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "          0         1         2         3\n",
       "0 -1.890151  0.571061  0.559870  0.037247\n",
       "1 -0.948316  0.448350 -0.172367 -1.315398\n",
       "2 -0.851705 -0.404880 -1.166217 -1.948257\n",
       "3 -0.692222 -0.605974  0.194261 -1.239979\n",
       "4 -0.617703  0.886264  0.495815  1.162216"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.890151</td>\n",
       "      <td>0.571061</td>\n",
       "      <td>0.559870</td>\n",
       "      <td>0.037247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.948316</td>\n",
       "      <td>0.448350</td>\n",
       "      <td>-0.172367</td>\n",
       "      <td>-1.315398</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.851705</td>\n",
       "      <td>-0.404880</td>\n",
       "      <td>-1.166217</td>\n",
       "      <td>-1.948257</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.692222</td>\n",
       "      <td>-0.605974</td>\n",
       "      <td>0.194261</td>\n",
       "      <td>-1.239979</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.617703</td>\n",
       "      <td>0.886264</td>\n",
       "      <td>0.495815</td>\n",
       "      <td>1.162216</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:15:34.377028Z",
     "start_time": "2025-02-26T13:15:34.370108Z"
    }
   },
   "cell_type": "code",
   "source": "t.loc[1] #单独把某一行取出来,类型是series",
   "id": "a00e4fcd10479f3c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    4\n",
       "1    5\n",
       "2    6\n",
       "3    7\n",
       "Name: 1, dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 列表套字典  变df",
   "id": "435bcbefa6d77667"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:16:16.141337Z",
     "start_time": "2025-02-26T13:16:16.104693Z"
    }
   },
   "cell_type": "code",
   "source": [
    "d2 =[{\"name\" : \"xiaohong\" ,\"age\" :32,\"tel\" :10010},\n",
    "     { \"name\": \"xiaogang\" ,\"tel\": 10000} ,\n",
    "     {\"name\":\"xiaowang\" ,\"age\":22}]\n",
    "df6=pd.DataFrame(d2) #列表套字典变为df,记作df6\n",
    "print(df6) #打印df6，缺失值会用NaN填充\n",
    "print(type(df6.values)) #ndarray"
   ],
   "id": "81fd3865e30f7b7c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       name   age      tel\n",
      "0  xiaohong  32.0  10010.0\n",
      "1  xiaogang   NaN  10000.0\n",
      "2  xiaowang  22.0      NaN\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:17:30.052607Z",
     "start_time": "2025-02-26T13:17:30.027803Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pd.Series(1, index=list(range(3,7)),dtype='float32')\n",
    "# 所有元素值均为 1（但由于 dtype 指定为 float32，实际存储的是 1.0）\n",
    "#索引设置：使用 index=list(range(3,7)) 生成索引 [3,4,5,6]"
   ],
   "id": "b8496d6736e07d9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3    1.0\n",
       "4    1.0\n",
       "5    1.0\n",
       "6    1.0\n",
       "dtype: float32"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# df中不同列可以是不同的数据类型,同一列必须是一个数据类型",
   "id": "30535de40f7949fa"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:24:56.279910Z",
     "start_time": "2025-02-26T13:24:56.136596Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "dict_data = {'A': 1,\n",
    "             'B': pd.Timestamp('20190926'),\n",
    "             'C': pd.Series(1, index=list(range(4)),dtype='float32'),\n",
    "             'D': np.array([1,2,3,4],dtype='int32'),\n",
    "             'E': [\"Python\",\"Java\",\"C++\",\"C\"],\n",
    "             'F': 'wangdao' }\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "print(df_obj2)\n",
    "print('-'*50)\n",
    "print(df_obj2.index) #行索引,重点\n",
    "#补课改变\n",
    "# df_obj2.index[0]=2  不可以单独修改某个索引值\n",
    "print(df_obj2.columns) #列索引，重点\n",
    "df_obj2.dtypes #每一列的数据类型，重点,不同列可以是不同的数据类型"
   ],
   "id": "6708ce6a208b0f35",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  4       C  wangdao\n",
      "--------------------------------------------------\n",
      "Index([0, 1, 2, 3], dtype='int64')\n",
      "Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "A            int64\n",
       "B    datetime64[s]\n",
       "C          float32\n",
       "D            int32\n",
       "E           object\n",
       "F           object\n",
       "dtype: object"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 日期,初始化df，设置行索引，列索引",
   "id": "d3c78dcb39c205df"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:25:35.172247Z",
     "start_time": "2025-02-26T13:25:35.129052Z"
    }
   },
   "cell_type": "code",
   "source": [
    "dates = pd.date_range('20230101', periods=6) #生成6个日期\n",
    "df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD')) #索引设置为日期\n",
    "print(df)\n",
    "print('-'*50)\n",
    "print(df.index)"
   ],
   "id": "d424c090b110657f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   A         B         C         D\n",
      "2023-01-01 -0.753957  2.226455  0.065162 -0.160978\n",
      "2023-01-02  0.938767 -1.035766 -2.348024 -1.911165\n",
      "2023-01-03 -2.147704 -0.668828  0.657135 -1.485708\n",
      "2023-01-04 -0.637523  2.488558  0.170472  0.500534\n",
      "2023-01-05  0.506597  0.123673  0.370856 -1.558104\n",
      "2023-01-06  0.229201 -0.785001 -0.572304  0.618670\n",
      "--------------------------------------------------\n",
      "DatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04',\n",
      "               '2023-01-05', '2023-01-06'],\n",
      "              dtype='datetime64[ns]', freq='D')\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:27:14.752217Z",
     "start_time": "2025-02-26T13:27:14.732477Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#取数据\n",
    "print(df_obj2) #打印df_obj2\n",
    "print(type(df_obj2)) #类型是DataFrame\n",
    "#pd中使用索引名来取某一行，或者列\n",
    "print(df_obj2['B'])\n",
    "#把df的某一列取出来是series\n",
    "print(type(df_obj2['B']))"
   ],
   "id": "738462087e3b4e38",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  4       C  wangdao\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "0   2019-09-26\n",
      "1   2019-09-26\n",
      "2   2019-09-26\n",
      "3   2019-09-26\n",
      "Name: B, dtype: datetime64[s]\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:29:03.884249Z",
     "start_time": "2025-02-26T13:29:03.873664Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#增加列数据，列名是自定义的\n",
    "df_obj2['ZHH'] = df_obj2['D'] + 4 #每次想向后添加直接修改df_obj2['ZHH']里面内容即可\n",
    "print(df_obj2.head(n=3))"
   ],
   "id": "85b3d61eab69eba7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F  HH  ZHH\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao   5    5\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao   6    6\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao   7    7\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:30:31.320554Z",
     "start_time": "2025-02-26T13:30:31.300830Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 删除列\n",
    "del(df_obj2['HH'])\n",
    "print(df_obj2.head())\n"
   ],
   "id": "ee8bcf2436e6ef9a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F  ZHH\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao    5\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao    6\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao    7\n",
      "3  1 2019-09-26  1.0  4       C  wangdao    8\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4 Pandas的索引操作",
   "id": "a5491e1058748ec7"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:33:37.889775Z",
     "start_time": "2025-02-26T13:33:37.844976Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "dict_data_ZZH = {'A': 1,\n",
    "             'B': pd.Timestamp('20190926'),\n",
    "             'C': pd.Series(1, index=list(range(4)),dtype='float32'),\n",
    "             'D': np.array([1,2,3,4],dtype='int32'),\n",
    "             'E': [\"Python\",\"Java\",\"C++\",\"C\"],\n",
    "             'F': 'wangdao' }\n",
    "df_obj2 = pd.DataFrame(dict_data_ZZH)\n",
    "print(df_obj2.index)"
   ],
   "id": "34698a78b93b6f3d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index([0, 1, 2, 3], dtype='int64')\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:33:53.302080Z",
     "start_time": "2025-02-26T13:33:53.268298Z"
    }
   },
   "cell_type": "code",
   "source": "df_obj2",
   "id": "ae455d13154e8afd",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   A          B    C  D       E        F\n",
       "0  1 2019-09-26  1.0  1  Python  wangdao\n",
       "1  1 2019-09-26  1.0  2    Java  wangdao\n",
       "2  1 2019-09-26  1.0  3     C++  wangdao\n",
       "3  1 2019-09-26  1.0  4       C  wangdao"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-09-26</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Python</td>\n",
       "      <td>wangdao</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-09-26</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>Java</td>\n",
       "      <td>wangdao</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-09-26</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>C++</td>\n",
       "      <td>wangdao</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-09-26</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4</td>\n",
       "      <td>C</td>\n",
       "      <td>wangdao</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "常见的Index种类\n",
    "•Index，索引  可以是各种类型\n",
    "•Int64Index，整数索引\n",
    "•MultiIndex，层级索引，难度较大\n",
    "•DatetimeIndex，时间戳类型"
   ],
   "id": "8df323a9acb778fc"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:34:28.344406Z",
     "start_time": "2025-02-26T13:34:28.321237Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# INDEX\n",
    "ser_obj = pd.Series(range(5), index = list(\"abcde\")) #索引是list,abcde\n",
    "print(ser_obj)\n",
    "ser_obj.index"
   ],
   "id": "12c5721fb0183af2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c', 'd', 'e'], dtype='object')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:36:13.809918Z",
     "start_time": "2025-02-26T13:36:13.798960Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 行索引，不仅可以用索引名获取元素，可以用索引位置或来取元素\n",
    "print(ser_obj['b']) #索引名\n",
    "print(ser_obj.iloc[2]) #索引位置"
   ],
   "id": "30785e76f2603ad6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:36:43.361422Z",
     "start_time": "2025-02-26T13:36:43.333380Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 切片索引\n",
    "print(ser_obj[1:3])  #索引位置取数据，左闭右开，打印第2-3个元素\n",
    "print(ser_obj['b':'d'])  #记住索引名  左闭右闭"
   ],
   "id": "36139ce0be640e31",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:37:56.990915Z",
     "start_time": "2025-02-26T13:37:56.960523Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 不连续索引\n",
    "print(ser_obj.iloc[[0, 2, 4]]) #打印元素0 2 4对应的索引表\n",
    "print(ser_obj[['a', 'e']]) #打印索引 a e对应的索引表"
   ],
   "id": "5f64ac829275b753",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "c    2\n",
      "e    4\n",
      "dtype: int64\n",
      "a    0\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:39:47.572511Z",
     "start_time": "2025-02-26T13:39:47.542623Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 布尔索引\n",
    "ser_bool = ser_obj > 2\n",
    "print(ser_obj)\n",
    "print(ser_bool) #布尔索引，True为1，False为0\n",
    "print('-'*50)\n",
    "print(ser_obj[ser_bool])\n",
    "\n",
    "print(ser_obj[ser_obj > 2]) #取出大于2的元素"
   ],
   "id": "8c823c4b896a51b3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "a    False\n",
      "b    False\n",
      "c    False\n",
      "d     True\n",
      "e     True\n",
      "dtype: bool\n",
      "--------------------------------------------------\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4.4 DataFrame索引",
   "id": "8af386c010ddb452"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:41:38.757524Z",
     "start_time": "2025-02-26T13:41:38.719660Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "df_obj = pd.DataFrame(np.random.randn(5,4),\n",
    "                      columns = ['a', 'b', 'c', 'd'])\n",
    "print(df_obj.head())"
   ],
   "id": "4893d5e55a54078e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "0 -1.711930 -0.094064 -0.552306 -0.332243\n",
      "1 -1.092489  2.578175  1.054653  0.394427\n",
      "2 -1.848583 -1.236265  1.180004  1.012513\n",
      "3 -1.445186  0.995667  0.108442  0.158358\n",
      "4 -0.119565  0.142349 -1.352464 -0.013416\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:41:47.618975Z",
     "start_time": "2025-02-26T13:41:47.588362Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 列索引\n",
    "print(df_obj['a']) # 返回Series类型\n",
    "print('-'*50)\n",
    "print(df_obj[['a']]) # 返回DataFrame类型\n",
    "print('-'*50)"
   ],
   "id": "64fb84078ec377bf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -1.711930\n",
      "1   -1.092489\n",
      "2   -1.848583\n",
      "3   -1.445186\n",
      "4   -0.119565\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "          a\n",
      "0 -1.711930\n",
      "1 -1.092489\n",
      "2 -1.848583\n",
      "3 -1.445186\n",
      "4 -0.119565\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:42:13.293814Z",
     "start_time": "2025-02-26T13:42:13.279878Z"
    }
   },
   "cell_type": "code",
   "source": "print(type(df_obj[['a']])) # 返回DataFrame类型",
   "id": "d501f651e4a06d13",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "loc 标签索引(通过索引标签值获取数据)",
   "id": "cba3b3311d5df82f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:42:44.407079Z",
     "start_time": "2025-02-26T13:42:44.398836Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 标签索引 loc，建议使用loc，效率更高\n",
    "# Series\n",
    "print(ser_obj)\n",
    "print(ser_obj.loc['b':'d']) #前闭后闭\n",
    "print('-'*50)"
   ],
   "id": "4d61c6e7cd81d498",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:43:27.073269Z",
     "start_time": "2025-02-26T13:43:27.045369Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame\n",
    "df_obj = pd.DataFrame(np.random.randn(5,4),\n",
    "                      columns = list('abcd'),\n",
    "                      index=list('abcde'))\n",
    "print(df_obj)\n",
    "print('-'*50)\n",
    "print(df_obj['a'])  #,拿列\n",
    "print('-'*50)\n",
    "print(df_obj.loc['a'])  #拿行\n",
    "print('-'*50)"
   ],
   "id": "7117b0da6b5d8130",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a -1.410864 -0.276454 -0.258887  0.041559\n",
      "b  0.530632  0.202217 -0.148976 -0.571435\n",
      "c  0.232229  1.544128  0.005919  0.635317\n",
      "d -0.079685  0.919718  0.570198  0.377960\n",
      "e -2.241326  0.076307  0.790232 -1.093635\n",
      "--------------------------------------------------\n",
      "a   -1.410864\n",
      "b    0.530632\n",
      "c    0.232229\n",
      "d   -0.079685\n",
      "e   -2.241326\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "a   -1.410864\n",
      "b   -0.276454\n",
      "c   -0.258887\n",
      "d    0.041559\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:43:55.946846Z",
     "start_time": "2025-02-26T13:43:55.914775Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 第一个参数索引行，第二个参数是列,loc或者iloc效率高于直接用取下标的方式，前闭后闭\n",
    "print(df_obj.loc['a':'c', 'b':'d']) #连续索引\n",
    "print(df_obj.loc[['a','c'], ['b','d']]) #不连续索引\n",
    "print(df_obj.loc[['c'],['b']]) #取一个值,返回的是DataFrame类型\n",
    "print(df_obj.loc['c','b'])  #取一个值"
   ],
   "id": "abe5a9f28a7d4e8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          b         c         d\n",
      "a -0.276454 -0.258887  0.041559\n",
      "b  0.202217 -0.148976 -0.571435\n",
      "c  1.544128  0.005919  0.635317\n",
      "          b         d\n",
      "a -0.276454  0.041559\n",
      "c  1.544128  0.635317\n",
      "          b\n",
      "c  1.544128\n",
      "1.5441278736809303\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## iloc 位置索引",
   "id": "92b64bc8f564d34b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:44:27.196535Z",
     "start_time": "2025-02-26T13:44:27.180834Z"
    }
   },
   "cell_type": "code",
   "source": "ser_obj",
   "id": "257b4c1eaf2c0ce6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    0\n",
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:44:39.437109Z",
     "start_time": "2025-02-26T13:44:39.426143Z"
    }
   },
   "cell_type": "code",
   "source": "df_obj",
   "id": "32dc61e821cd13b9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          a         b         c         d\n",
       "a -1.410864 -0.276454 -0.258887  0.041559\n",
       "b  0.530632  0.202217 -0.148976 -0.571435\n",
       "c  0.232229  1.544128  0.005919  0.635317\n",
       "d -0.079685  0.919718  0.570198  0.377960\n",
       "e -2.241326  0.076307  0.790232 -1.093635"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>-1.410864</td>\n",
       "      <td>-0.276454</td>\n",
       "      <td>-0.258887</td>\n",
       "      <td>0.041559</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>0.530632</td>\n",
       "      <td>0.202217</td>\n",
       "      <td>-0.148976</td>\n",
       "      <td>-0.571435</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>0.232229</td>\n",
       "      <td>1.544128</td>\n",
       "      <td>0.005919</td>\n",
       "      <td>0.635317</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>-0.079685</td>\n",
       "      <td>0.919718</td>\n",
       "      <td>0.570198</td>\n",
       "      <td>0.377960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>-2.241326</td>\n",
       "      <td>0.076307</td>\n",
       "      <td>0.790232</td>\n",
       "      <td>-1.093635</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:44:50.659599Z",
     "start_time": "2025-02-26T13:44:50.651767Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Series\n",
    "print(ser_obj[1:3])\n",
    "print(ser_obj.iloc[1:3]) # 前闭后开[)，效率高\n",
    "print('-'*50)"
   ],
   "id": "6b231a954423fc1a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:45:01.249755Z",
     "start_time": "2025-02-26T13:45:01.237158Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame，iloc是前闭后开[)\n",
    "print(df_obj)\n",
    "print(df_obj.iloc[0:2, 0:2]) # 注意和df_obj.loc[0:2, 'a']的区别\n",
    "print(df_obj.iloc[[0,2], [0,2]]) # 不连续索引\n",
    "print(df_obj.iloc[0,0]) # 取一个值"
   ],
   "id": "de1a9489a670bbe4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a -1.410864 -0.276454 -0.258887  0.041559\n",
      "b  0.530632  0.202217 -0.148976 -0.571435\n",
      "c  0.232229  1.544128  0.005919  0.635317\n",
      "d -0.079685  0.919718  0.570198  0.377960\n",
      "e -2.241326  0.076307  0.790232 -1.093635\n",
      "          a         b\n",
      "a -1.410864 -0.276454\n",
      "b  0.530632  0.202217\n",
      "          a         c\n",
      "a -1.410864 -0.258887\n",
      "c  0.232229  0.005919\n",
      "-1.4108639433897698\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:45:13.199773Z",
     "start_time": "2025-02-26T13:45:13.186568Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#没有设置行和列索引的DataFrame，iloc和loc的区别\n",
    "df_obj2 = pd.DataFrame(np.random.randn(5,4))\n",
    "print(df_obj2)\n",
    "print('-'*50)\n",
    "print(df_obj2.iloc[0:2]) #左闭右开\n",
    "print('-'*50)\n",
    "print(df_obj2.loc[0:2]) #左闭右闭"
   ],
   "id": "b9b7c71dd1f51e52",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0 -1.859907  0.453966 -1.035880 -1.003417\n",
      "1  1.458467 -0.245859 -1.209705  0.496930\n",
      "2 -0.849716  0.700045 -0.239689  0.340693\n",
      "3  0.045896  0.691057  0.746869  0.346162\n",
      "4  0.650073 -1.363342  0.162923 -1.211595\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0 -1.859907  0.453966 -1.035880 -1.003417\n",
      "1  1.458467 -0.245859 -1.209705  0.496930\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0 -1.859907  0.453966 -1.035880 -1.003417\n",
      "1  1.458467 -0.245859 -1.209705  0.496930\n",
      "2 -0.849716  0.700045 -0.239689  0.340693\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 函数应用 apply（） map（）",
   "id": "2d41febb4068c59d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:50:19.154496Z",
     "start_time": "2025-02-26T13:50:18.991565Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# Numpy ufunc 函数，randn跟的是维数\n",
    "df = pd.DataFrame(np.random.randn(5,4) - 1)\n",
    "print(df)\n",
    "\n",
    "print(np.abs(df)) #绝对值"
   ],
   "id": "a8015f22170c6bd2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0 -1.106803 -2.668704 -1.784050 -0.231180\n",
      "1 -0.220874 -1.412714 -1.123329 -2.019827\n",
      "2 -1.992883 -1.755370 -0.679216  0.591553\n",
      "3 -0.196282  0.898453 -1.176042 -0.569797\n",
      "4 -1.051242 -0.321407  1.232326 -1.837070\n",
      "          0         1         2         3\n",
      "0  1.106803  2.668704  1.784050  0.231180\n",
      "1  0.220874  1.412714  1.123329  2.019827\n",
      "2  1.992883  1.755370  0.679216  0.591553\n",
      "3  0.196282  0.898453  1.176042  0.569797\n",
      "4  1.051242  0.321407  1.232326  1.837070\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:50:31.526270Z",
     "start_time": "2025-02-26T13:50:31.438887Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#apply默认作用在列上,x是每一列,因为axis=0\n",
    "print(df.apply(lambda x : x.max(),axis=0)) #每一列的最大值"
   ],
   "id": "e04a019f15e39ede",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -0.196282\n",
      "1    0.898453\n",
      "2    1.232326\n",
      "3    0.591553\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:50:51.952962Z",
     "start_time": "2025-02-26T13:50:51.937674Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#apply作用在行上\n",
    "print(df.apply(lambda x : x.max(), axis=1)) #每一行的最大值"
   ],
   "id": "2f9ff46ee2f58232",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -0.231180\n",
      "1   -0.220874\n",
      "2    0.591553\n",
      "3    0.898453\n",
      "4    1.232326\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:51:20.924836Z",
     "start_time": "2025-02-26T13:51:20.890614Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用map应用到每个数据\n",
    "print(df.map(lambda x : '%.2f' % x))\n",
    "df.dtypes"
   ],
   "id": "516b1e67d041411",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2      3\n",
      "0  -1.11  -2.67  -1.78  -0.23\n",
      "1  -0.22  -1.41  -1.12  -2.02\n",
      "2  -1.99  -1.76  -0.68   0.59\n",
      "3  -0.20   0.90  -1.18  -0.57\n",
      "4  -1.05  -0.32   1.23  -1.84\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0    float64\n",
       "1    float64\n",
       "2    float64\n",
       "3    float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 空值处理",
   "id": "c1376927dd6cf3b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:51:56.709718Z",
     "start_time": "2025-02-26T13:51:56.685557Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_data = pd.DataFrame([np.random.randn(3), [1., 2., np.nan],\n",
    "                       [np.nan, 4., np.nan], [1., 2., 3.]])\n",
    "print(df_data.head())  # 打印数组"
   ],
   "id": "cc4d4742c7e96815",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0 -1.567189  1.186333  0.734714\n",
      "1  1.000000  2.000000       NaN\n",
      "2       NaN  4.000000       NaN\n",
      "3  1.000000  2.000000  3.000000\n"
     ]
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:53:00.893276Z",
     "start_time": "2025-02-26T13:53:00.887582Z"
    }
   },
   "cell_type": "code",
   "source": "df_data.iloc[2,0] # 取第2行第0列的值",
   "id": "79b99b2fe7fd8ea2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(nan)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:53:17.756307Z",
     "start_time": "2025-02-26T13:53:17.732952Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#isnull来判断是否有空的数据\n",
    "print(df_data.isnull())\n",
    "#计算每一列的缺失率\n",
    "print(df_data.isnull().sum()/df_data.shape[0])"
   ],
   "id": "764328fedcd5fc2a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2\n",
      "0  False  False  False\n",
      "1  False  False   True\n",
      "2   True  False   True\n",
      "3  False  False  False\n",
      "0    0.25\n",
      "1    0.00\n",
      "2    0.50\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "删除缺失数据",
   "id": "279275c9e2e8e4e9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:54:17.223098Z",
     "start_time": "2025-02-26T13:54:17.164054Z"
    }
   },
   "cell_type": "code",
   "source": "print(df_data.dropna(subset=[0]))",
   "id": "20315d00b3f2e21b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0 -1.567189  1.186333  0.734714\n",
      "1  1.000000  2.000000       NaN\n",
      "3  1.000000  2.000000  3.000000\n"
     ]
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:54:33.828291Z",
     "start_time": "2025-02-26T13:54:33.812695Z"
    }
   },
   "cell_type": "code",
   "source": "df_data",
   "id": "9f90d4a7195aac8d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          0         1         2\n",
       "0 -1.567189  1.186333  0.734714\n",
       "1  1.000000  2.000000       NaN\n",
       "2       NaN  4.000000       NaN\n",
       "3  1.000000  2.000000  3.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.567189</td>\n",
       "      <td>1.186333</td>\n",
       "      <td>0.734714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:54:50.545993Z",
     "start_time": "2025-02-26T13:54:50.532682Z"
    }
   },
   "cell_type": "code",
   "source": "print(df_data.dropna(axis=1))  #某列有nan就删除该列",
   "id": "1e69014b0c9a1842",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          1\n",
      "0  1.186333\n",
      "1  2.000000\n",
      "2  4.000000\n",
      "3  2.000000\n"
     ]
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 填充缺失数据",
   "id": "de3c50b997bd650e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:55:37.299675Z",
     "start_time": "2025-02-26T13:55:37.282964Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# fillna()给零列的空值填为-100，按特征（按列）去填充\n",
    "print(df_data.iloc[:,0].fillna(-100.))\n",
    "df_data"
   ],
   "id": "9c1e73737ce6777b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     -1.567189\n",
      "1      1.000000\n",
      "2   -100.000000\n",
      "3      1.000000\n",
      "Name: 0, dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "          0         1         2\n",
       "0 -1.567189  1.186333  0.734714\n",
       "1  1.000000  2.000000       NaN\n",
       "2       NaN  4.000000       NaN\n",
       "3  1.000000  2.000000  3.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.567189</td>\n",
       "      <td>1.186333</td>\n",
       "      <td>0.734714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 51
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 分组",
   "id": "741d510f6ec010d3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:58:24.021778Z",
     "start_time": "2025-02-26T13:58:23.948924Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "#分组后给名称加前缀\n",
    "dict_obj = {'key1' : ['a', 'b', 'a', 'b',\n",
    "                      'a', 'b', 'a', 'a'],\n",
    "            'key2' : ['one', 'one', 'two', 'three',\n",
    "                      'two', 'two', 'one', 'three'],\n",
    "            'data1': np.random.randint(1, 10, 8),\n",
    "            'data2': np.random.randint(1, 10, 8)}\n",
    "df_obj = pd.DataFrame(dict_obj)\n",
    "print(df_obj)"
   ],
   "id": "c72269e55dd1dc7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key1   key2  data1  data2\n",
      "0    a    one      3      8\n",
      "1    b    one      8      8\n",
      "2    a    two      8      2\n",
      "3    b  three      9      8\n",
      "4    a    two      3      1\n",
      "5    b    two      7      5\n",
      "6    a    one      8      6\n",
      "7    a  three      6      1\n"
     ]
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:58:41.195332Z",
     "start_time": "2025-02-26T13:58:41.123619Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('-'*50)\n",
    "# 按key1分组后，计算data1，data2的统计信息并附加到原始表格中，并添加表头前缀\n",
    "k1_sum = df_obj.groupby('key1').mean(numeric_only=True).add_prefix('mean_')\n",
    "print(k1_sum)"
   ],
   "id": "c4e0115f1ae1f9d4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "      mean_data1  mean_data2\n",
      "key1                        \n",
      "a            5.6         3.6\n",
      "b            8.0         7.0\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T13:58:59.035191Z",
     "start_time": "2025-02-26T13:58:59.005031Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 方法2，使用transform，分组后计算结果和原本的df保持一致\n",
    "k1_sum_tf = df_obj.loc[:, ['key1','data1', 'data2']].groupby('key1').transform('mean').add_prefix('mean_')\n",
    "k1_sum_tf"
   ],
   "id": "dbfe954d2bc47b59",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   mean_data1  mean_data2\n",
       "0         5.6         3.6\n",
       "1         8.0         7.0\n",
       "2         5.6         3.6\n",
       "3         8.0         7.0\n",
       "4         5.6         3.6\n",
       "5         8.0         7.0\n",
       "6         5.6         3.6\n",
       "7         5.6         3.6"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mean_data1</th>\n",
       "      <th>mean_data2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.6</td>\n",
       "      <td>3.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>8.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5.6</td>\n",
       "      <td>3.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>8.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.6</td>\n",
       "      <td>3.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>8.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>5.6</td>\n",
       "      <td>3.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5.6</td>\n",
       "      <td>3.6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 重复值处理",
   "id": "266e577681e46544"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T14:01:26.280069Z",
     "start_time": "2025-02-26T14:01:26.263100Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_obj = pd.DataFrame({'data1' : ['a'] * 4 + ['b'] * 4,\n",
    "                       'data2' : np.random.randint(0, 4, 8)})\n",
    "print(df_obj)\n",
    "print('-'*50)\n",
    "print(df_obj.duplicated())\n",
    "print('-'*50)\n",
    "df_obj[~df_obj.duplicated()]  #取出不重复行"
   ],
   "id": "2b7f7ee14c0f9eb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  data1  data2\n",
      "0     a      1\n",
      "1     a      0\n",
      "2     a      1\n",
      "3     a      0\n",
      "4     b      3\n",
      "5     b      0\n",
      "6     b      3\n",
      "7     b      0\n",
      "--------------------------------------------------\n",
      "0    False\n",
      "1    False\n",
      "2     True\n",
      "3     True\n",
      "4    False\n",
      "5    False\n",
      "6     True\n",
      "7     True\n",
      "dtype: bool\n",
      "--------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "  data1  data2\n",
       "0     a      1\n",
       "1     a      0\n",
       "4     b      3\n",
       "5     b      0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>data1</th>\n",
       "      <th>data2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>a</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>b</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 58
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 按照某一列去重",
   "id": "7f8ac92d455480b9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T14:01:59.819237Z",
     "start_time": "2025-02-26T14:01:59.794952Z"
    }
   },
   "cell_type": "code",
   "source": "print(df_obj.duplicated('data2')) #按照data2列去重",
   "id": "761763ee0575f1b6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1    False\n",
      "2     True\n",
      "3     True\n",
      "4    False\n",
      "5     True\n",
      "6     True\n",
      "7     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 59
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T14:02:20.135374Z",
     "start_time": "2025-02-26T14:02:20.124643Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_obj1 = pd.DataFrame({'data1' :[np.nan] * 4,'data2' :list('1225')}) # 将data1中的元素全部变为NaN,data2中变为列表1225\n",
    "df_obj1"
   ],
   "id": "92868a6f6596eef3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   data1 data2\n",
       "0    NaN     1\n",
       "1    NaN     2\n",
       "2    NaN     2\n",
       "3    NaN     5"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>data1</th>\n",
       "      <th>data2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 60
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T14:03:45.688092Z",
     "start_time": "2025-02-26T14:03:45.676751Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#在pd的duplicated认为空值和空值相等的\n",
    "df_obj1.duplicated('data1')"
   ],
   "id": "298298f026d7187e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    False\n",
       "1     True\n",
       "2     True\n",
       "3     True\n",
       "dtype: bool"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T14:04:39.300687Z",
     "start_time": "2025-02-26T14:04:39.289169Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df_obj1.drop_duplicates('data1'))\n",
    "print('-'*50)\n",
    "print(df_obj.drop_duplicates())  #删除重复行"
   ],
   "id": "bb5d352096f9be15",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   data1 data2\n",
      "0    NaN     1\n",
      "--------------------------------------------------\n",
      "  data1  data2\n",
      "0     a      1\n",
      "1     a      0\n",
      "4     b      3\n",
      "5     b      0\n"
     ]
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 如果要在原有的df上去重，需要加inplace=True",
   "id": "8e4595e67038d5d2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T14:05:22.521676Z",
     "start_time": "2025-02-26T14:05:22.511377Z"
    }
   },
   "cell_type": "code",
   "source": "df_obj",
   "id": "bc4be86240d851c7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  data1  data2\n",
       "0     a      1\n",
       "1     a      0\n",
       "2     a      1\n",
       "3     a      0\n",
       "4     b      3\n",
       "5     b      0\n",
       "6     b      3\n",
       "7     b      0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>data1</th>\n",
       "      <th>data2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>a</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>a</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>b</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>b</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>b</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 65
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T14:05:38.595557Z",
     "start_time": "2025-02-26T14:05:38.581686Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# map与applymap一样，但是map只能用于series，applymap只能用于df\n",
    "ser_obj = pd.Series(np.random.randint(0,10,10))  #series 用map\n",
    "print(ser_obj)\n",
    "# map() 方法遍历 Series 的每个元素\n",
    "# lambda x: x ** 2 是匿名函数，输入参数 x 表示每个元素值，返回 x 的平方\n",
    "print(ser_obj.map(lambda x : x ** 2)) #对 ser_obj 这个 Series 中的每个元素执行平方运算"
   ],
   "id": "261c0ae0e8779af7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    6\n",
      "1    5\n",
      "2    8\n",
      "3    7\n",
      "4    4\n",
      "5    7\n",
      "6    5\n",
      "7    5\n",
      "8    8\n",
      "9    1\n",
      "dtype: int32\n",
      "0    36\n",
      "1    25\n",
      "2    64\n",
      "3    49\n",
      "4    16\n",
      "5    49\n",
      "6    25\n",
      "7    25\n",
      "8    64\n",
      "9     1\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 66
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
